package com.fun.crazyThread;

import java.util.LinkedList;

public class MyTask {
    private final LinkedList<String> list = new LinkedList<>();

    public synchronized void push(String o) {
        System.out.println("push");
        synchronized (list) {
            list.addLast(o);
            notify();
        }
    }

    public synchronized Object pop() throws InterruptedException {
        System.out.println("pop");
        synchronized (list) {
            if (list.size() <= 0) {
                wait();
            }
            System.out.println("pop finish");
            return list.removeLast();
        }
    }

    public static void main(String[] args) throws InterruptedException {
        final MyTask deadLockTest = new MyTask();
        Thread thread = new Thread(new Runnable() {
            public void run() {
                try {
                    deadLockTest.pop();
                } catch (Exception e) {
// TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        thread.start();
        Thread.sleep(2000);
        Thread threadPush = new Thread(new Runnable() {
            public void run() {
                try {
                    deadLockTest.push("push thread");
                } catch (Exception e) {
// TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
        threadPush.start();

    }
}
